<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>建立一个数组，包括变量名和它们的值</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.compact" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">compact</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">compact</span> &mdash; <span class="dc-title">
   建立一个数组，包括变量名和它们的值
  </span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.compact-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>compact</strong></span>
    ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$varname1</code></span>
   [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$...</code></span>
    ] ) : <span class="type">array</span></div>

    <p class="para rdfs-comment">
     创建一个包含变量与其值的数组。
    </p>
    <p class="para">
     对每个参数，<span class="function"><strong>compact()</strong></span>
     在当前的符号表中查找该变量名并将它添加到输出的数组中，变量名成为键名而变量的内容成为该键的值。简单说，它做的事和
     <span class="function"><a href="extract.html" class="function">extract()</a></span> 正好相反。返回将所有变量添加进去后的数组。
    </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    在 PHP 7.3 之前版本，未设置的字符串会被静默忽略。
   </p>
  </p></blockquote> 
 </div>
 
 <div class="refsect1 parameters" id="refsect1-function.compact-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">varname1</code></dt>

     <dd>

      <p class="para">
       <span class="function"><strong>compact()</strong></span> 接受可变的参数数目。每个参数可以是一个包括变量名的字符串或者是一个包含变量名的数组，该数组中还可以包含其它单元内容为变量名的数组，
 <span class="function"><strong>compact()</strong></span> 可以递归处理。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.compact-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回输出的数组，包含了添加的所有变量。
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.compact-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   如果字符串指向的变量未定义，<span class="function"><strong>compact()</strong></span> 会产生 E_NOTICE 级错误。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.compact-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>7.3.0</td>
      <td>
        现在，如果字符串指向的变量未定义，<span class="function"><strong>compact()</strong></span> 会产生 E_NOTICE 级错误。
       在此之前，此类问题会静默忽略掉。
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>

 
 <div class="refsect1 examples" id="refsect1-function.compact-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-6089">
    <p><strong>Example #1 <span class="function"><strong>compact()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$city&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"San&nbsp;Francisco"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$state&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"CA"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$event&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"SIGGRAPH"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$location_vars&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"city"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"state"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">compact</span><span style="color: #007700">(</span><span style="color: #DD0000">"event"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"nothing_here"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$location_vars</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [event] =&gt; SIGGRAPH
    [city] =&gt; San Francisco
    [state] =&gt; CA
)
</pre></div>
    </div>
   </div>
  </p>
 </div>

 <div class="refsect1 notes" id="refsect1-function.compact-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <strong>Gotcha</strong><br />
   <p class="para">
    因为<a href="language.variables.variable.html" class="link">可变变量</a>也许不能在函数内部用于
  PHP 的<a href="language.variables.superglobals.html" class="link">超全局数组</a>，此时不能将超全局数组传递入
  <span class="function"><strong>compact()</strong></span> 中。
   </p>
  </p></blockquote>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.compact-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="extract.html" class="function" rel="rdfs-seeAlso">extract()</a> - 从数组中将变量导入到当前的符号表</span></li>
   </ul>
  </p>
 </div>
   

</div></div></div></body></html>